<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>问答机器人</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #74ebd5, #acb6e5);
            margin: 0;
            padding: 20px;
        }

 #chat-container {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    padding: 20px;
    max-width: 600px;
    margin: auto;
    height: 800px; /* 增加容器的高度 */
    display: flex;
    flex-direction: column; /* 使内容垂直排列 */
}

#messages {
    flex: 1; /* 使消息区域占据剩余空间 */
    max-height: 700px; /* 增加最大高度 */
    overflow-y: auto;
    margin-bottom: 20px;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
}

#input-container {
    display: flex;
    margin-top: auto; /* 使输入框和按钮固定在底部 */
}
        .message {
            margin: 5px 0;
            padding: 10px;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .user {
            background-color: #d1e7dd;
            align-self: flex-end;
        }
        .bot {
            background-color: #f8d7da;
            align-self: flex-start;
        }
        #user-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;

        }
        #send-btn {
            padding: 10px;
            margin-left: 10px;
            border: none;
            background-color: #007bff;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }
        #send-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div id="chat-container">
        <h2>问答机器人</h2>
        <div id="messages"></div>
        <div id="input-container">
            <input type="text" id="user-input" placeholder="请问有什么问题？" />
            <button id="send-btn">发送</button>
        </div>
    </div>

    <script>
        document.getElementById('send-btn').onclick = function() {
            const userInput = document.getElementById('user-input').value.trim();
            if (userInput === "") return;

            addMessage(userInput, 'user');
            document.getElementById('user-input').value = '';

            // 发送请求到后端
            fetch('http://localhost:5000/api/ask', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ messages: [{ role: 'user', content: userInput }] }),
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    addMessage(data.error, 'bot');
                } else {
                    typeOutMessage(data.answer, 'bot'); // 使用逐字输出
                }
            })
            .catch(error => {
                console.error('Error:', error);
                addMessage('发生错误，请稍后再试。', 'bot');
            });
        };

        function addMessage(text, sender) {
            const messagesDiv = document.getElementById('messages');
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message ' + sender;
            messageDiv.textContent = text;
            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight; // 滚动到底部
        }

        function typeOutMessage(text, sender) {
            const messagesDiv = document.getElementById('messages');
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message ' + sender;

            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight; // 滚动到底部

            let index = 0;
            const interval = setInterval(() => {
                if (index < text.length) {
                    messageDiv.textContent += text.charAt(index);
                    index++;
                } else {
                    clearInterval(interval); // 完成输出后停止
                }
            }, 100); // 每100毫秒输出一个字
        }

        document.getElementById('user-input').addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                document.getElementById('send-btn').click();
            }
        });
    </script>
</body>
</html>